/**
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jimm.protocols.icq.packet.received.ssi;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.util.Log;

import org.jimm.protocols.icq.contacts.Contact;
import org.jimm.protocols.icq.core.OscarConnection;
import org.jimm.protocols.icq.exceptions.BadPacketException;
import org.jimm.protocols.icq.exceptions.ConvertStringException;
import org.jimm.protocols.icq.integration.events.ContactListEvent;
import org.jimm.protocols.icq.integration.listeners.ContactListListener;
import org.jimm.protocols.icq.packet.received.ReceivedPacket;
import org.jimm.protocols.icq.tool.StringTools;

/**
 * <p>
 * Created by
 * 
 * @author Lo�c Broquet
 * @author Коновалов Алексей (aleks_dsp)
 */
public final class SsiContactListReply__19_6 extends ReceivedPacket {

	private static final String LOG_TAG = "ICQ:SsiContactListReply";

	// типы записей списка
	private static final int TYPE_CONTACT = 0x0000;
	private static final int TYPE_GROUP = 0x0001;

	// типы TLV-полей
	private static final int TLV_TYPE_NICK = 0x0131;

	private int count;

	// private int lastChangeTime;

	private ArrayList<Contact> contacts = new ArrayList<Contact>();

	public SsiContactListReply__19_6(DataInputStream strm)
			throws ConvertStringException, IOException, BadPacketException {
		super(strm, true);

		byte data[] = getSnac().getDataFieldByteArray();
		DataInputStream dataStrm = new DataInputStream(
				new ByteArrayInputStream(data));
		dataStrm.skip(8 + 1);
		count = dataStrm.readUnsignedShort();
		
		HashMap<Integer, String> groups = new HashMap<Integer, String>();
		ArrayList<Integer> contactGroups = new ArrayList<Integer>();  

		for (int i = 0; i < count; ++i) {
			int nameLen = dataStrm.readUnsignedShort();
			String name = StringTools.utf8ByteArrayToString(dataStrm, nameLen);

			int group = dataStrm.readUnsignedShort();
			//int id = dataStrm.readUnsignedShort();
			dataStrm.skip(2);
			int type = dataStrm.readUnsignedShort();

			// get length of the following TLVs
			int len = dataStrm.readUnsignedShort();
			switch (type) {
			case TYPE_CONTACT:
				String nick = name;

				// Читаем все Tlv
				while (len > 0) {
					int tlvType = dataStrm.readUnsignedShort();
					int tlvLength = dataStrm.readUnsignedShort();

					switch (tlvType) {
					case TLV_TYPE_NICK:
						nick = StringTools.utf8ByteArrayToString(dataStrm,
								tlvLength);
						break;
					default:
						dataStrm.skip(tlvLength);
					}
					len -= tlvLength + 4;
				}
				contactGroups.add(group);
				contacts.add(new Contact(nick, name));
				break;

			case TYPE_GROUP:
				groups.put(group, name);
				dataStrm.skip(len);
				break;
			default:
				// skip TLVs
				dataStrm.skip(len);
			}
		}

		int contactsCount = contacts.size();
		for(int i = 0; i < contactsCount; ++i) {
			String groupName = groups.get(contactGroups.get(i));
			if (groupName != null)
				contacts.get(i).group = groupName;
		}
		// lastChangeTime = dataStrm.readInt();
	}

	public void execute(OscarConnection connection) {
	}

	public void notifyEvent(OscarConnection connection) {
		ContactListEvent e = new ContactListEvent(this, contacts);

		for (ContactListListener l : connection.getContactListListeners()) {
			Log.d(LOG_TAG, "notify listener " + l.getClass().getName()
					+ " onUpdateContactList()");
			l.onUpdateContactList(e);
		}
	}
}
